package com.lyw.dubbo;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import org.springframework.context.annotation.Bean;

public class RpcCommonConfig {
    // 连接注册中心配置
    @Bean("dubboRegistry")
    public RegistryConfig registryConfig() {
        RegistryConfig registryConfig = new RegistryConfig();
        String rergistryAddress = "redis://127.0.0.1:6379";
        if (StringUtils.isEmpty(rergistryAddress)) {
            throw new IllegalArgumentException("需要配置 dubbo.registry.address");
        }
        registryConfig.setAddress(rergistryAddress);
        String username = "any";
        String password = "123456";
        registryConfig.setUsername(username);
        registryConfig.setPassword(password);
        return registryConfig;
    }

    // 当前应用配置
    @Bean("dubboApplication")
    public ApplicationConfig applicationConfig() {
        ApplicationConfig applicationConfig = new ApplicationConfig();
        String applicationName = "dubbo";

        applicationConfig.setName(applicationName);
        applicationConfig.setLogger("slf4j");
        return applicationConfig;
    }

    // 服务提供者协议配置
    @Bean("dubboProtocol")
    public ProtocolConfig protocolConfig() {
        ProtocolConfig protocol = new ProtocolConfig();
        protocol.setName("dubbo");
        int port = 20880;

        protocol.setPort(port);
        int threads = 100;

        protocol.setThreads(threads);
        String serverImpl = "netty4";

        protocol.setServer(serverImpl);
        String clientImpl = "netty4";

        protocol.setClient(clientImpl);
        String threadPool = "fixed";

        protocol.setThreadpool(threadPool);
//        protocol.setSerialization("kryo");
        return protocol;
    }
}
